﻿using DevExpress.XtraEditors;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XCI.Extensions;
using XCI.Helper;

namespace XCI.Windows.Controls
{
    [System.ComponentModel.DesignerCategory("Code")]
    public class CheckedListBoxControlx : CheckedListBoxControl
    {
        private object _lazyEditValue;

        /// <summary>
        /// 获取或设置指定当前选定值。
        /// </summary>
        [Category("Data"),DefaultValue(""), Description("获取或设置指定当前选定值。")][Bindable(true)]
        public virtual object EditValue
        {
            get
            {
                var isDataBinding = DataSource != null;
                if (isDataBinding && string.IsNullOrEmpty(ValueMember))
                    throw new InvalidOperationException("请设置ValueMember");
                if (this.CheckedItems.Count > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (var item in CheckedItems)
                    {
                        if (item != null)
                        {
                            sb.Append(isDataBinding
                                ? $"{ObjectHelper.GetObjectProperty(item, ValueMember)},"
                                : $"{item},");
                        }
                    }
                    return sb.ToString().TrimEnd(',');
                }
                return string.Empty;
            }
            set
            {
                _lazyEditValue = value;
                var isDataBinding = DataSource != null;
                if (value==null || string.IsNullOrEmpty(value.ToString()))
                {
                    if (!isDataBinding)
                    {
                        for (int i = 0; i < Items.Count; i++)
                        {
                            SetItemChecked(i, false);
                        }
                    }
                    else
                    {
                        for (int i = 0; i < ItemCount; i++)
                        {
                            SetItemChecked(i, false);
                        }
                    }
                    return;
                }
               
                var sz = value.ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (!isDataBinding)
                {
                    for (int i = 0; i < Items.Count; i++)
                    {
                        var item = Items[i];
                        if (item.Value == null)
                        {
                            continue;
                        }

                        SetItemChecked(i, sz.Any(p => p.Equals(item.Value.ToString(), StringComparison.OrdinalIgnoreCase)));
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(ValueMember))
                        throw new InvalidOperationException("请设置ValueMember");
                    for (int i = 0; i < ItemCount; i++)
                    {
                        var item = GetItem(i);
                        var val = ObjectHelper.GetObjectProperty(item, ValueMember);
                        if (val == null)
                        {
                            continue;
                        }

                        SetItemChecked(i, sz.Any(p => p.Equals(val.ToStringOrEmpty(), StringComparison.OrdinalIgnoreCase)));
                    }
                }
            }
        }

        /// <summary>
        /// 数据源
        /// </summary>
        [Browsable(false), DefaultValue(null), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override object DataSource
        {
            get { return base.DataSource; }
            set
            {
                base.DataSource = value;
                if (_lazyEditValue != null)
                {
                    EditValue = _lazyEditValue;
                }
            }
        }
    }
}
